const app = getApp()

Page({
  data: {
  },

  onLoad: function () {
  },

  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      // 获取到用户的信息了，打印到控制台上看下
      console.log("用户的信息如下：");
      console.log(e.detail.userInfo);
      app.globalData.ifshowthetap=true;
      wx.login({
        success(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'https://localhost:8080/Landing_request/',
              data: {
                code: res.code
              },
              success(res) {
                if (res.data == '0') {
                  wx.showToast({
                    title: '登录失败ERR:0'
                  })
                }
                console.log('原版：' + res.data.uptime)
                //根据返回的json武装本地存储和内存
                // console.log(app.globalData.Landingtime)
                app.globalData.pwdWithSalt = res.data.pwdWithSalt
                app.globalData.Landingtime = res.data.uptime
                // console.log(app.globalData.Landingtime)
                //存入本地缓存
                wx.setStorage({
                  key: 'pwdWithSalt',
                  data: app.globalData.pwdWithSalt
                })
                wx.setStorage({
                  key: 'Landingtime',
                  data: app.globalData.Landingtime
                })
                //页面返回
                wx.navigateBack({
                  delta: 1,
                  success(res) {
                    wx.request({
                      url: 'https://localhost:8080/save_user_info/',
                      data: {
                        data: e.detail.userInfo,
                        pwdWithSalt: app.globalData.pwdWithSalt
                      },
                    })
                    wx.request({
                      url: 'https://localhost:8080/getInfoToLocal/',
                      data: {
                        pwdWithSalt: app.globalData.pwdWithSalt
                      },
                      success(res) {
                        app.globalData.admin = res.data.admin
                        //存入本地缓存
                        wx.setStorage({
                          key: 'admin',
                          data: res.data.admin
                        })
                        // 测试用
                        // wx.getStorage({
                        //   key:'admin',
                        //   success(res){
                        //     console.log(res.data)
                        //   }
                        // })
                      },
                      fail(res) {
                        console.log(res)
                      }

                    })
                  }
                })
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })

      try {
        wx.setStorageSync('nikeName', e.detail.userInfo.nickName)
        wx.setStorageSync('gender', e.detail.userInfo.gender)
        wx.setStorageSync('language', e.detail.userInfo.language)
        wx.setStorageSync('city', e.detail.userInfo.city)
        wx.setStorageSync('province', e.detail.userInfo.province)
        // var value = wx.getStorageSync('province')
        // console.log(value)
      } catch (e) {
        console.log(e)
      }
      wx.showToast({
        title: '正在连接服务器',
        icon: 'loading',
      })
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入.',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  }
  // bindAdminLanding: function (e){
  //   console.log('打开admin')
  //   wx.navigateTo({
  //     url: '../AdminRgst/AdminRgst',
  //     success: function (res) {
  //       console.log(res)
  //     },
  //     fail:function(res){
  //       wx.showToast({
  //         title:'调用登陆失败',
  //         icon:'loading',
  //         duration:1000
  //       })
  //     }
  //   })
  // }
})